﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HotDealD2.Models.ModelView;

namespace HotDealD2.Models.ModelManager
{
    public class PromotionModel
    {
        private QLDataBaseDataContext dataContext;
        public PromotionModel()
        {
            dataContext = new QLDataBaseDataContext();
        }
        public  List<PromotionStatisticView> GetListPromotion(string username)
        {
            List<PromotionStatisticView> listPromotion = new List<PromotionStatisticView>();
            listPromotion.AddRange(GetListFromDealChuaDuyet(username));
            listPromotion.AddRange(GetListFromDealDaDuyet(username));
            listPromotion.AddRange(GetListFromDeal(username));
            return listPromotion;
        }
        public List<PromotionStatisticView> GetListFromDealChuaDuyet(string username)
        {
            List<PromotionStatisticView> listPromotion = new List<PromotionStatisticView>();
            List<DealChuaDuyet> list = (from pro in dataContext.DealChuaDuyets
                                        where pro.UsernameDoanhNghiep == username
                                        select pro).ToList();

            foreach (DealChuaDuyet deal in list)
            {
                PromotionStatisticView pro = new PromotionStatisticView();
                pro.TenDeal = deal.TenDeal;
                pro.TinhTrang = "Chưa duyệt";
                pro.IDPromotion = null;
                pro.TongSoDonHang = 0;
                pro.TinhTrangGiaTot = "Chưa tốt";
                pro.ThongKeSoTien = String.Format("{0:0,0}", 0);
                pro.HanSuDung = deal.ThoiHanSuDungVoucher;

                listPromotion.Add(pro);
            }
            return listPromotion;
        }
        public List<PromotionStatisticView> GetListFromDealDaDuyet(string username)
        {
            List<PromotionStatisticView> listPromotion = new List<PromotionStatisticView>();
            List<DealDaDuyet> list = (from pro in dataContext.DealDaDuyets
                                        where pro.UsernameDoanhNghiep == username
                                        select pro).ToList();

            foreach (DealDaDuyet deal in list)
            {
                PromotionStatisticView pro = new PromotionStatisticView();
                pro.TenDeal = deal.TenDeal;
                pro.TinhTrang = "Đã duyệt";
                pro.IDPromotion = null;
                pro.TongSoDonHang = 0;
                pro.TinhTrangGiaTot = "Chưa tốt";
                pro.ThongKeSoTien = String.Format("{0:0,0}", 0);
                pro.HanSuDung = deal.ThoiHanSuDungVoucher;

                listPromotion.Add(pro);
            }
            return listPromotion;
        }
        public List<PromotionStatisticView> GetListFromDeal(string username)
        {
            List<PromotionStatisticView> listPromotion = new List<PromotionStatisticView>();
            List<Deal> list = (from pro in dataContext.Deals
                                        where pro.UsernameDoanhNghiep == username
                                        select pro).ToList();
            
            foreach (Deal deal in list)
            {
                PromotionStatisticView pro = new PromotionStatisticView();
                pro.TenDeal = deal.TenDeal;
                pro.IDPromotion = deal.MaDeal;
                pro.TinhTrang = "Đang bán";
                pro.TongSoDonHang = deal.SoLuongMuaHienTai;
                pro.TinhTrangGiaTot = deal.SoLuongMuaHienTai > deal.SoLuongMuaGiaTot? "Đạt giá tốt": "Chưa tốt";
                pro.GiaTien = String.Format("{0:0,0}",deal.GiaTien);
                pro.ThongKeSoTien = String.Format("{0:0,0}", deal.SoLuongMuaHienTai * decimal.ToInt32(deal.GiaTien));
                
                pro.HanSuDung = deal.ThoiHanSuDungVoucher;
                listPromotion.Add(pro);
            }
            return listPromotion;
        }
        
        public PromotionStatisticView GetPromotion(string username, int maDeal)
        {
            PromotionStatisticView promotion = new PromotionStatisticView();
            Deal deal = (from pro in dataContext.Deals
                               where pro.UsernameDoanhNghiep == username && pro.MaDeal == maDeal
                               select pro).First();

            promotion.TenDeal = deal.TenDeal;
            promotion.IDPromotion = deal.MaDeal;
            promotion.TinhTrang = "Đang bán";
            promotion.TongSoDonHang = deal.SoLuongMuaHienTai;
            promotion.TinhTrangGiaTot = deal.SoLuongMuaHienTai > deal.SoLuongMuaGiaTot ? "Đạt giá tốt" : "Chưa tốt";
            promotion.GiaTien = String.Format("{0:0,0}", deal.GiaTien);
            promotion.ThongKeSoTien = String.Format("{0:0,0}", deal.SoLuongMuaHienTai * decimal.ToInt32(deal.GiaTien));
            promotion.HanSuDung = deal.ThoiHanSuDungVoucher;
            
            return promotion;
           
        }
    }
}